home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / test / testcache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  4.9 KB  |  271 lines

  1. /*
  2.  * testcache.c --
  3.  *    Cache test code.
  4.  */
  5.  
  6. #include <stdio.h>
  7.  
  8. #include "c.h"
  9.  
  10. #include "catname.h"
  11. #include "ftup.h"
  12. #include "heapam.h"
  13. #include "log.h"
  14. #include "syscache.h"
  15. #include "tupdesc.h"
  16.  
  17. #ifndef    COMPLICATED
  18. #define TESTKEY    "cachelang"
  19. #define TESTDAT    "initial-cachelang-path"
  20. #endif
  21.  
  22. RcsId("$Header: /private/postgres/src/test/RCS/testcache.c,v 1.4 1990/02/12 19:51:58 cimarron Version_2 $");
  23.  
  24. static struct catcache    *TestSysCache = NULL;
  25.  
  26. /*
  27.  * DoAddTuple --
  28.  */
  29. extern
  30. void
  31. DoAddTuple ARGS((
  32.     Relation    relation,
  33.     HeapTuple    tuple
  34. ));
  35.  
  36. /*
  37.  * DoSearchTestCache --
  38.  *    True iff tuple is found.
  39.  */
  40. extern
  41. bool
  42. DoSearchTestCache ARGS((
  43.     Relation    relation,
  44.     int        id
  45. ));
  46.  
  47. /*
  48.  * DoReplace --
  49.  */
  50. extern
  51. void
  52. DoReplace ARGS((
  53.     Relation    relation
  54. ));
  55.  
  56. /*
  57.  * DoShowTuple --
  58.  */
  59. extern
  60. void
  61. DoShowTuple ARGS((
  62.     Relation    relation,
  63.     HeapTuple    tuple
  64. ));
  65.  
  66. /*
  67.  * DoShowAttributes --
  68.  */
  69. extern
  70. void
  71. DoShowAttributes ARGS((
  72.     Relation    relation
  73. ));
  74.  
  75. TestMain()
  76. {
  77.     HeapTuple    tuple;
  78.     Relation    relation;
  79.     bool        immediateFlush = true;
  80.  
  81.     static NameData    relationNameData;
  82.     static int    beenHere = 0;
  83.  
  84.     StartTransactionCommand();
  85.     if (beenHere == 0) {
  86.         beenHere = 1;
  87.  
  88. #ifdef    COMPLICATED
  89.         puts("Please enter a catalog relation name");
  90.         if (scanf("%s", relationNameData.data) != 1) {
  91.             elog(NOTICE, "testcache: no relation specified");
  92.             exitpg(0);
  93.         }
  94.         tuple = NULL;        /* XXX */
  95.         relation = NULL;    /* XXX */
  96. #else
  97.         strncpy(relationNameData.data, LanguageRelationName, 16);
  98.         {
  99.             Datum    datum[2];
  100.  
  101.             datum[0] = NameGetDatum(TESTKEY);
  102.             datum[1] = PointerGetDatum((Pointer)textin(TESTDAT));
  103.             relation = RelationNameOpenHeapRelation(
  104.                 LanguageRelationName);
  105.  
  106.             tuple = FormHeapTuple(2,
  107.                 RelationGetTupleDescriptor(relation), datum,
  108.                 "  ");
  109.         }
  110. #endif
  111.         DoAddTuple(relation, tuple);
  112.         CommitTransactionCommand();
  113.         StartTransactionCommand();
  114.  
  115.         beenHere = 2;
  116.  
  117.     } else if (beenHere > 1) {
  118.         elog(FATAL, "testcache: giving up!");
  119.  
  120.     } else if (beenHere == 1) {
  121.         elog(NOTICE, "testcache: %s relation may exist???--continuing!",
  122.             relationNameData.data);
  123.  
  124.         beenHere = 2;
  125.     }
  126.  
  127.     /* one time processing */
  128.     DoShowAttributes(relation);
  129.     if (!DoSearchTestCache(relation, LANNAME)) {
  130.         Assert(RelationIsValid(relation));
  131.         Assert(HeapTupleIsValid(tuple));
  132.  
  133.         DoAddTuple(relation, tuple);
  134.     }
  135.     CommitTransactionCommand();
  136.     StartTransactionCommand();
  137.     srand(time(0));
  138.  
  139. #ifdef    COMPLICATED
  140.         Assert(0);
  141. #endif
  142.  
  143.     for (;;) {
  144. /*
  145.         puts("\nPlease enter an operator, flags, and an integer value");
  146.         status = scanf("%s %d %d", operatorNameData.data, &flags,
  147.             &returnType);
  148.  
  149.         if (status <= 0) {
  150.             break;
  151.         } else if (status != 3) {
  152.             elog(NOTICE, "testbtree: improper qualification specified");
  153.             exitpg(0);
  154.         }
  155. */
  156.         puts("*****************************************");
  157.         fprintf(stderr, "Search for it:\n");
  158.         DoSearchTestCache(relation, LANNAME);
  159.  
  160.         if (immediateFlush) {
  161.             SetHeapAccessMethodImmediateInvalidation(true);
  162.         }
  163.         fprintf(stderr, "Replace it:\n");
  164.         DoReplace(relation);
  165.         if (immediateFlush) {
  166.             SetHeapAccessMethodImmediateInvalidation(false);
  167.         }
  168.  
  169.         fprintf(stderr, "Search for it again:\n");
  170.         DoSearchTestCache(relation, LANNAME);
  171.  
  172.         CommitTransactionCommand();
  173.         puts("");
  174.         sleep(1);
  175.         StartTransactionCommand();
  176.  
  177.         immediateFlush = (bool)!immediateFlush;
  178.     }
  179.  
  180.     RelationCloseHeapRelation(relation);
  181.  
  182.     puts("\nDone!");
  183.  
  184.     endmmgr(NULL);
  185. }
  186.  
  187. void
  188. DoAddTuple(relation, tuple)
  189.     Relation    relation;
  190.     HeapTuple    tuple;
  191. {
  192.     /* someday, check that the tuple does not already exist */
  193.  
  194.     RelationInsertHeapTuple(relation, tuple);
  195. }
  196.  
  197. bool
  198. DoSearchTestCache(relation, id)
  199.     Relation    relation;
  200.     int        id;
  201. {
  202.     HeapTuple    tuple;
  203.  
  204.     Assert(RelationIsValid(relation));
  205.  
  206.     setheapoverride(true);
  207.     tuple = SearchSysCacheTuple(id, TESTKEY);
  208.     setheapoverride(false);
  209.     if (!HeapTupleIsValid) {
  210.         puts("\t<tuple-not-found>");
  211.         return (false);
  212.     } else {
  213.         DoShowTuple(relation, tuple);
  214.     }
  215.     return (true);
  216. }
  217.  
  218. void
  219. DoReplace(relation)
  220.     Relation    relation;
  221. {
  222.     HeapTuple    oldTuple;
  223.     HeapTuple    newTuple;
  224.     Datum        datum[2];
  225.     char        path[80];
  226.  
  227.     Assert(RelationIsValid(relation));
  228.  
  229.     sprintf(path, "%d", rand());
  230.     datum[0] = NameGetDatum(TESTKEY);
  231.     datum[1] = PointerGetDatum((Pointer)textin(path));
  232.     newTuple = FormHeapTuple(2, RelationGetTupleDescriptor(relation),
  233.         datum, "  ");
  234.  
  235.     fprintf(stderr, "Replaced it with: %s\n", path);
  236.     oldTuple = SearchSysCacheTuple(LANNAME, TESTKEY);
  237. /*
  238.     SetRefreshWhenInvalidate(true);
  239. */
  240.     RelationReplaceHeapTuple(relation, &oldTuple->t_ctid, newTuple,
  241.         (double *)NULL);
  242. /*
  243.     SetRefreshWhenInvalidate(false);
  244. */
  245. }
  246.  
  247. void
  248. DoShowTuple(relation, tuple)
  249.     Relation    relation;
  250.     HeapTuple    tuple;
  251. {
  252.     if (!HeapTupleIsValid(tuple)) {
  253.         puts("\t<nil>");
  254.         return;
  255.     }
  256.     if (ItemPointerIsValid(&tuple->t_ctid)) {
  257.         printf("\t[%d,%d]\n",
  258.             ItemPointerSimpleGetPageNumber(&tuple->t_ctid),
  259.             ItemPointerSimpleGetOffsetNumber(&tuple->t_ctid));
  260.     }
  261.     debugtup(tuple, RelationGetTupleDescriptor(relation));
  262. }
  263.  
  264. void
  265. DoShowAttributes(relation)
  266.     Relation    relation;
  267. {
  268.     showatts(&relation->rd_rel->relname, relation->rd_rel->relnatts,
  269.         RelationGetTupleDescriptor(relation));
  270. }
  271.